<script setup>
import {onMounted, ref} from 'vue'
import {get,post} from '@/utils/http'
import {CircleCheck, Lock, User} from '@element-plus/icons-vue'

const src = ref()
const key = ref()

function getCaptcha() {
  get('/check').then(res => {
    src.value = res.check
    key.value = res.checkkey
  })
}

onMounted(getCaptcha)

const form = ref({
  account: 'admin',
  password: '123456',
  captcha: '',
})


async function login() {
  let {account, password, captcha} = form.value
  const res = await post("/user/login", {account, password, value: captcha, id: key.value})
  if (res.code === 200) {
    ElMessage({type: 'success', message: res.msg})

  } else if (res.code === 400) {
    ElMessage({type: 'warning', message: res.msg})
  } else {
    ElMessage({type: 'error', message: res.msg})
  }
}


</script>

<template>
  <div class="box">
    <div class="container">
      <el-form v-model="form">
        <el-row>
          <el-col>
            <el-input size="large" :prefix-icon="User" type="text" v-model="form.account" placeholder="账号"></el-input>
          </el-col>
          <el-col>
            <el-input size="large" :prefix-icon="Lock" show-password type="password" v-model="form.password" placeholder="密码"></el-input>
          </el-col>
          <el-col>
            <el-input size="large" :prefix-icon="CircleCheck" type="text" v-model="form.captcha" placeholder="验证码">
              <template #suffix><img style="cursor: pointer" title="点击换一张" :src="src" alt="" @click="getCaptcha"></template>
            </el-input>
          </el-col>
          <el-col>
            <el-button size="large" type="primary" style="width:100%" @click="login">登录</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<style scoped>
.box {
  background-color: #333;
  background-image: url('@/assets/bg.jpg');
  background-repeat: no-repeat;
  background-size: 100% 100%;
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.container {
  width: 390px;
}

.el-col {
  margin: 3px;
}
</style>
